package com.hm.system.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.CommonUtil;
import com.hm.common.model.ControllerResult;
import com.hm.common.model.PageInfo;
import com.hm.system.mongo.vo.system.SysRoleInfoVo;
import com.hm.system.share.service.mongo.system.SysRoleInfoService;
import com.hm.system.util.SystemConfigDef.Api.Rest;
import com.hm.system.web.su.BaseController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 2016年5月12日 下午9:59:25
 * @version 1.0
 * @describe 系统角色
 */
@ResponseBody
@RestController
@RequestMapping(value = Rest.Sys.REST_SYS_ROLE_INFO)
public class SysRoleInfoController extends BaseController{

	public static final String swaggerTag = "系统角色管理";

	@Autowired
	private SysRoleInfoService sysRoleInfoService;
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/{roleId}", method = RequestMethod.GET)
	@ApiOperation(value = "获取系统角色", httpMethod = "GET", notes = "获取系统角色通过ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统角色", content = "获取系统角色通过ID")
	public ResponseEntity<ControllerResult<SysRoleInfoVo>> getSysRoleInfo(
			@ApiParam(required = true, name = "roleId", value = "系统角色ID") @PathVariable String roleId) {
		try {
			return ControllerResult.success(sysRoleInfoService.getSysRoleInfo(roleId));
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "添加系统角色", httpMethod = "POST", notes = "添加系统角色返回添加成功的角色ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "添加系统角色", content = "添加系统角色返回添加成功的角色ID")
	public ResponseEntity<ControllerResult<String>> addSysRoleInfo(
			@ApiParam(required = true, name = "body", value = "系统角色信息") @RequestBody(required = true) SysRoleInfoVo sysRoleInfo) {
		try {
			if(CommonUtil.isEmpty(sysRoleInfo.getName())){
				return ControllerResult.failed("角色名称不允许为null");
			}
			String id = sysRoleInfoService.addSysRoleInfo(sysRoleInfo, currentUserId());
			return ControllerResult.success(id, "添加成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{roleId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改系统角色", httpMethod = "PUT", notes = "修改系统角色返回修改成功的角色ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改系统角色", content = "修改系统角色返回修改成功的角色ID")
	public ResponseEntity<ControllerResult<String>> updateSysRoleInfo(
			@ApiParam(required = true, name = "body", value = "系统角色信息") @RequestBody(required = true) SysRoleInfoVo sysRoleInfo,
			@ApiParam(required = true, name = "roleId", value = "系统角色ID") @PathVariable String roleId) {
		try {
			if(CommonUtil.isEmpty(sysRoleInfo.getName())){
				return ControllerResult.failed("角色名称不允许为null");
			}
			sysRoleInfo.setId(roleId);
			String id = sysRoleInfoService.updateSysRoleInfo(sysRoleInfo, currentUserId());
			return ControllerResult.success(id, "修改成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{roleId}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除系统角色", httpMethod = "DELETE", notes = "删除系统角色", tags = swaggerTag)
	@RequestPlanAnnotation(title = "删除系统角色", content = "修改系统角色")
	public ResponseEntity<ControllerResult<String>> deleteSysRoleInfo(
			@ApiParam(required = true, name = "roleId", value = "系统角色ID") @PathVariable String roleId) {
		try {
			SysRoleInfoVo sysRoleInfo = sysRoleInfoService.getSysRoleInfo(roleId);
			if(null == sysRoleInfo){
				return ControllerResult.failed("目标删除数据不存在,删除失败");
			}
			
			String id = sysRoleInfoService.deleteSysRoleInfo(sysRoleInfo, currentUserId());;
			return ControllerResult.success(id, "删除成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(value = "模糊查询系统角色数据", httpMethod = "GET", notes = "分页获取系统角色数据", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统角色数据", content = "分页获取系统角色数据")
	public ResponseEntity<ControllerResult<PageInfo<SysRoleInfoVo>>> querySysRoleInfo(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "name", value = "角色名") @RequestParam(required = false) String name,
			@ApiParam(required = false, name = "startTime", value = "开始时间") @RequestParam(required = false) Long startTime,
			@ApiParam(required = false, name = "endTime", value = "结束时间") @RequestParam(required = false) Long endTime) {
		try {
			PageInfo<SysRoleInfoVo> page = sysRoleInfoService.querySysRoleInfo(name, startTime, endTime, pageNumber, pageSize);
			return ControllerResult.success(
					page);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
}
